//
//  StatusRetweetView.swift
//  SinaWeibo
//
//  Created by Tony-ztb on 15/12/21.
//  Copyright © 2015年 Tony-ztb. All rights reserved.
//

import UIKit
import SnapKit

class StatusRetweetView: UIView {

    // 转发视图底部约束的属性
    var retweetViewBottomConstraints: Constraint?
    
    var retweetedStatus: Status? {
        didSet {
            
            
            retweetedLabel.text = "@" + (retweetedStatus?.user?.name ?? "") + " " + (retweetedStatus?.text ?? "")
            
            pictureView.imageURLs = retweetedStatus?.imageURLs
            
            // 卸掉之前的约束
            self.retweetViewBottomConstraints?.uninstall()
            // 根据是否又配图  重新添加约束
            if let urls = retweetedStatus?.imageURLs where urls.count > 0 {
                // 有配图
                self.snp_makeConstraints { (make) -> Void in
                    self.retweetViewBottomConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
                }
            } else {
                // 没有配图
                self.snp_makeConstraints { (make) -> Void in
                    self.retweetViewBottomConstraints = make.bottom.equalTo(retweetedLabel.snp_bottom).offset(StatusCellMargin).constraint
                }
            }
        }
    }
    
    // MARK: 重写构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = UIColor(white: 0.96, alpha: 1)
        
        // 设置子视图
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI() {
        
        addSubview(retweetedLabel)
        addSubview(pictureView)
        
        // 设置约束
        retweetedLabel.snp_makeConstraints { (make) -> Void in
            make.top.left.equalTo(self).offset(StatusCellMargin)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(retweetedLabel.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(retweetedLabel.snp_left)
        }
        
        // 自动计算行高
        self.snp_makeConstraints { (make) -> Void in
            self.retweetViewBottomConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
        }
    }

    
    
    // MARK: 懒加载子视图
    private lazy var retweetedLabel: UILabel = UILabel(title: "转发微博", color: UIColor.darkGrayColor(), fontSize: 14, margin: StatusCellMargin)
    // 配图视图
    private lazy var pictureView: StatusCellPictureView = StatusCellPictureView()
    
}
